package com.shadowlight.hero_story.util;

import com.shadowlight.hero_story.config.AllConfig;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @Description redis工具
 * @Author cz
 * @Date 2022/3/22
 */
@Slf4j
public final class RedisUtil {

    private RedisUtil() {
    }

    private static JedisPool jedisPool = null;

    public static void init() {
        try {
            jedisPool = new JedisPool(AllConfig.REDIS_SERVER_HOST, AllConfig.REDIS_SERVER_PORT);
            log.info("redis连接成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public static Jedis getRedis() {
        if (null == jedisPool) {
            throw new RuntimeException("_jedisPool 尚未初始化");
        }

        Jedis jedis = jedisPool.getResource();
        jedis.auth(AllConfig.REDIS_PASSWORD);
        return jedis;
    }
}
